package com.woniu.project.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.project.core.domain.ManagementRole;
import com.woniu.project.core.domain.Role;
import com.woniu.project.core.exception.ProjectAuthenException;
import com.woniu.project.dao.ManagementRoleMapper;
import com.woniu.project.dao.RoleMapper;
import com.woniu.project.dto.RoleDto;
import com.woniu.project.service.RoleService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Klaus
 * @since 2020-10-13
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {
    @Autowired
    private ManagementRoleMapper managementRoleMapper;

    @Autowired
    private RoleMapper roleMapper;

    /**
     * 根据用户ID，查询对应的角色数组
     * @param managementId
     * @return
     */
    @Override
    public List<RoleDto> selectRoleByManagementId(Integer managementId) {
        //查询出该管理员的所有角色ID
        List<ManagementRole> managementRoles = managementRoleMapper.selectRoleByManagementId(managementId);

        //根据角色ID查询对应的角色
        List<RoleDto> roleDtos = new ArrayList<>();
        for (ManagementRole managementRole : managementRoles) {
            Role role = roleMapper.selectById(managementRole.getRoleId());
            RoleDto roleDto = new RoleDto();
            BeanUtils.copyProperties(role,roleDto);
            roleDtos.add(roleDto);
        }
        return roleDtos;
    }
}
